package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/3/26
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;


    @Override
    public PageResult findItemCatList(PageResult pageResult) {
        //进行分页查询
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        IPage page = new Page(pageResult.getPageNum(), pageResult.getPageSize());
        page = itemCatMapper.selectPage(page,queryWrapper);

        //获取分页后的一级菜单数据,并且实现数据封装
        List<ItemCat> oneItemCatList = page.getRecords();
        oneItemCatList = findChildrenList(oneItemCatList,3);
        pageResult.setTotal(page.getTotal())
                  .setRows(oneItemCatList);
        return pageResult;
    }

    @Override
    public void updateStatus(ItemCat itemCat) {

        itemCatMapper.updateById(itemCat);
    }

    public List<ItemCat> findOneLevelList(){
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        return itemCatMapper.selectList(queryWrapper);
    }

    public List<ItemCat> findChildrenList(List<ItemCat> itemCatList,Integer type){ //3
        for(ItemCat itemCat : itemCatList){
            if(itemCat.getLevel() < type){
            int parentId = itemCat.getId();
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",parentId);
            List<ItemCat> childrenList = itemCatMapper.selectList(queryWrapper);
            //查询下一级标签
            findChildrenList(childrenList,type);
            itemCat.setChildren(childrenList);
        }
    }
        return itemCatList;
    }

    @Override
    public List<ItemCat> findChildrenItemCatList(Integer type) {
        List<ItemCat> itemCatList = null;

        if(type == 1){ //表示只查询一级商品分类
            //1.查询1级商品分类信息
            itemCatList = findOneLevelList();
        }

        //表示查询2级商品分类信息
        if(type == 2){
            itemCatList = findOneLevelList();
            itemCatList = findChildrenList(itemCatList,2);
        }

        if(type == 3){
            itemCatList = findOneLevelList();
            itemCatList = findChildrenList(itemCatList,3);
        }
        return itemCatList;
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    public List<ItemCat> findItemCatListByParentId(int parentId) {
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", parentId);
        return itemCatMapper.selectList(queryWrapper);
    }

    @Override
    public void updateItemCat(ItemCat itemCat) {
        System.out.println(itemCat);
        itemCatMapper.updateById(itemCat);
    }


    @Override
    @Transactional
    public void deleteItemCats(Integer id, Integer level) {
        //如果3级分类,则直接删除
        if(level == 3){
            itemCatMapper.deleteById(id);
        }
        //如果是2级分类,则删除所有子级分类和自己
        if(level == 2){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            itemCatMapper.delete(queryWrapper);
            itemCatMapper.deleteById(id);
        }
        //如果是1级分类,则需要关联删除数据
        if(level == 1){
            //1.获取所有二级分类信息
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);

            //2.删除二级关联的所有三级分类和自己
            for(ItemCat itemCat2 : twoList){
                QueryWrapper<ItemCat> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("parent_id",itemCat2.getId());
                itemCatMapper.delete(queryWrapper2);
                itemCatMapper.deleteById(itemCat2.getId());
            }
            //3.删除1级商品分类的子级
            itemCatMapper.deleteById(id);
        }
    }
}
